package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a04_有效的括号复习4
 * </p>
 *
 * @author flyduck
 * @since 2025/5/16
 */
public class a04_有效的括号复习4 {
    //第一种情况：(]
    //第二种情况：())
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();

        for (char c : chars) {
            if(c == '('){
                stack.push(')');
            }else if(c == '['){
                stack.push(']');
            }else if(c == '{'){
                stack.push('}');
            }else {
                if(stack.isEmpty()){
                    return false;
                }
                if(!stack.peek().equals(c)){
                    return false;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
